package com.tentcoo.zhaogonggu.module.homepage.b;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;

import com.tentcoo.zhaogonggu.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Gunter on 2015/4/22.
 */
public class AttainDiamondActivity extends FragmentActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private ViewPager mViewPager;
    private DiamondAttainTab tab_recharge, tab_seekAward, tab_record;
    private List<DiamondAttainFragment> viewFragments;
    private List<DiamondAttainTab> tabViews;
    private int orange;
    private int[] tabLayouts = {R.layout.tab_recharge, R.layout.tab_seek_award, R.layout.tab_record};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diamond_attain);
        mViewPager = (ViewPager) findViewById(R.id.vp_diamond_attain);
        orange = Integer.parseInt("FF6600", 16);
        initViewFragments();
        initTabViews();
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return viewFragments.size();
            }

            @Override
            public Fragment getItem(int position) {
                return viewFragments.get(position);
            }
        });
        mViewPager.setOnPageChangeListener(this);
        mViewPager.setCurrentItem(0);
    }

    private void initTabViews() {
        tabViews = new ArrayList<>();
        tab_recharge = (DiamondAttainTab) findViewById(R.id.tab_recharge);
        tab_seekAward = (DiamondAttainTab) findViewById(R.id.tab_seek_award);
        tab_record = (DiamondAttainTab) findViewById(R.id.tab_record);
        tab_recharge.setOnClickListener(this);
        tab_seekAward.setOnClickListener(this);
        tab_record.setOnClickListener(this);
        tab_recharge.setAlpha(0xFF);
        tabViews.add(tab_recharge);
        tabViews.add(tab_seekAward);
        tabViews.add(tab_record);
    }

    private void initViewFragments() {
        viewFragments = new ArrayList<>();
        for (int i = 0; i < tabLayouts.length; i++) {
            DiamondAttainFragment viewFragment = new DiamondAttainFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(DiamondAttainFragment.KEY_LAYOUT_RES_ID, tabLayouts[i]);
            viewFragment.setArguments(bundle);
            viewFragments.add(viewFragment);
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (positionOffset > 0) {
            DiamondAttainTab frontTab = tabViews.get(position);
            DiamondAttainTab followingTab = tabViews.get(position + 1);

            int frontAlpha = (int)Math.ceil(255 * (1 - positionOffset));
            int followingAlpha = (int)Math.ceil(255*positionOffset);
            frontTab.setAlpha(frontAlpha);
            followingTab.setAlpha(followingAlpha);
        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        /*switch (v.getId()){
            case R.id.btn_recharge:
                btn_recharge.setAlpha(255);

                tabViews.get(mViewPager.getCurrentItem()).setAlpha(0);
                mViewPager.setCurrentItem(0);
                break;
            case R.id.btn_seek_award:
                btn_seekAward.setAlpha(255);
                tabViews.get(mViewPager.getCurrentItem()).setAlpha(0);
                mViewPager.setCurrentItem(1);
                break;
            case R.id.btn_record:
                btn_record.setAlpha(255);
                tabViews.get(mViewPager.getCurrentItem()).setAlpha(0);
                mViewPager.setCurrentItem(2);
                break;

        }*/
    }
}
